package com.huhao.concurrency.example.immutable;


import com.google.common.collect.Maps;
import com.huhao.concurrency.annoations.NotThreadSafe;
import com.huhao.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.Map;

/**
 * unmodifiableMap
 * 不可修改
 * 修改时不会编译报错，但是运行会报错抛出异常
 */
@Slf4j
@ThreadSafe
public class ImmutableUnmodifiableMap {
    private static Map<Integer, Integer> map = Maps.newHashMap();

    static {
        map.put(1, 2);
        map.put(3, 4);
        map.put(5, 6);
        //不可被修改
        map = Collections.unmodifiableMap(map);
    }

    public static void main(String[] args) {
        //unmodifiableMap后，修改会报错
        map.put(1, 3);
        log.info("{}", map.get(1));
        log.info("{}", map);
    }
}
